#include <bits/stdc++.h>
using namespace std;
const int inf = 1e9 + 10;
const int MAXN = 1e6 + 10;
const int base1 = 131, base2 = 13331;
const int P1 = 1e9 + 7, P2 = 1e9 + 9;

using PII = pair<int, int>;
using ll = long long;

ll H1A[MAXN], H2A[MAXN], H1B[MAXN], H2B[MAXN];

ll pow1[MAXN], pow2[MAXN];

void init() {
  pow1[0] = pow2[0] = 1;
  for (int i = 1; i < MAXN; i++) {
    pow1[i] = 1LL * pow1[i - 1] * base1 % P1;
    pow2[i] = 1LL * pow2[i - 1] * base2 % P2;
  }
}

// 合并get_a和get_b为一个通用函数
PII get_hash(ll h1[], ll h2[], int l, int r) {
  int t1 = 1LL * h1[l - 1] * pow1[r - l + 1] % P1;
  int v1 = 1LL * (h1[r] - t1 + P1) % P1;
  int t2 = 1LL * h2[l - 1] * pow2[r - l + 1] % P2;
  int v2 = 1LL * (h2[r] - t2 + P2) % P2;
  return {v1, v2};
}

void solve() {
  string a, b;
  cin >> a >> b;
  a = " " + a;
  b = " " + b;
  int n = a.size();
  int m = b.size();

  init();

  H1A[0] = H2A[0] = H1B[0] = H2B[0] = 0;
  for (int i = 1; i <= n; i++) {
    H1A[i] = (1LL * H1A[i - 1] * base1) % P1 + (a[i] - '0' + 1) % P1;
    H2A[i] = (1LL * H2A[i - 1] * base2) % P2 + (a[i] - '0' + 1) % P2;
    H1B[i] = (1LL * H1B[i - 1] * base1) % P1 + (b[i] - '0' + 1) % P1;
    H2B[i] = (1LL * H2B[i - 1] * base2) % P2 + (b[i] - '0' + 1) % P2;
  }
  int ans = inf;
  for (int i = 1; i <= n; i++) {

    auto part1_a = get_hash(H1A, H2A, i, n);
    auto part2_a = get_hash(H1A, H2A, 1, i - 1);

    auto part1_b = get_hash(H1B, H2B, 1, n - i + 1);
    auto part2_b = get_hash(H1B, H2B, n - i + 2, n);

    if (part1_a == part1_b && part2_a == part2_b) {
      ans = min(ans, i - 1);
    }
  }
  if (ans == inf)
    cout << "-1\n";
  else
    cout << ans << '\n';
}

int main() {
  int T;
  cin >> T;
  while (T--) {
    solve();
  }
  return 0;
}